//
//  MemberMessageViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/7/25.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "MemberInformationViewController.h"

#import "MemberModel.h"

#import "UIImageView+EMWebCache.h"

#import "ChatController.h"

@interface MemberInformationViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIImageView     *iconImageView;//头像
    UIImage         *headImg;
    
    NSString        *huanxinID;
}
@property (nonatomic, strong) UITableView * infoTableView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, strong) NSArray * cellTextArray;
@property (nonatomic, strong) NSArray * cellDetailTextArray;

@end

@implementation MemberInformationViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _cellTextArray = [NSArray array];
        _cellDetailTextArray = [NSArray array];
        _dataSource = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self getMemberData];
    
    [self creatTableView];

}

- (void)creatData
{
    
    _cellDetailTextArray = [NSMutableArray array];
    
    _cellTextArray = @[@"姓名",@"性别",@"电话",@"地址"];
    
    MemberModel *memberModel = _dataSource[0];
    _cellDetailTextArray = @[memberModel.truename.length>0?memberModel.truename:@"暂无信息",
                             [memberModel.sex intValue] == 1?@"男":@"女",
                             memberModel.phone.length   > 0? memberModel.phone : @"暂无信息",
                             memberModel.address.length > 0? memberModel.address : @"暂无信息"];
    //   _cellDetailTextArray = @[@"王小二",@"男",@"健康管家",@"59",@"南京市"];
    
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:memberModel.img] placeholderImage:[UIImage imageNamed:@"touxiang.jpg"]];
    
}

#pragma mark - --------------网络请求
- (void)getMemberData {
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    NSDictionary *parameters = @{@"hx_username":_hxUserID};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"search.hxuser.info" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"群成员信息的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1&&[[dictionary objectForKey:@"total"]intValue] > 0)
            
        {
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                MemberModel * memberModel = [[MemberModel alloc]init];
                [memberModel setValuesForKeysWithDictionary:dataDic];
                
                [_dataSource addObject:memberModel];
                
                huanxinID = memberModel.hx_username;
            }
            
            [self creatData];
            [_infoTableView reloadData];
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
    
}

- (void)creatTableView
{
    _infoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    _infoTableView.backgroundColor = [UIColor clearColor];
    _infoTableView.delegate = self;
    _infoTableView.dataSource = self;
    _infoTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:_infoTableView];
    
    
    UIView *tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 50)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
    _infoTableView.tableHeaderView = tableHeaderView;
    
    
    
    //头像view
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 50)];
    headerView.backgroundColor = [UIColor whiteColor];
    [tableHeaderView addSubview:headerView];
    
    //头像Label
    UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 40, 50)];
    headerLabel.text = @"头像";
    headerLabel.font = [UIFont systemFontOfSize:13];
    [headerView addSubview:headerLabel];
    
    //头像imageView
    iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W-15-20-15-50, 10, 50, 50)];
    iconImageView.layer.masksToBounds = YES;
    iconImageView.layer.cornerRadius = 25;
    iconImageView.userInteractionEnabled = YES;

    
    [headerView addSubview:iconImageView];
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 70)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
    _infoTableView.tableFooterView = footerView;
    
    UIButton *chatButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W/2-130, 10, 120, 50)];
    
    [chatButton setTitle:@"发消息" forState:UIControlStateNormal];
    [chatButton setTintColor:[UIColor blackColor]];
    [chatButton addTarget:self action:@selector(chatButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [chatButton setBackgroundColor:DarkGreencolor];
    
    chatButton.layer.cornerRadius = 5.0;
    chatButton.clipsToBounds = YES;
    
    [footerView addSubview:chatButton];
    
    
    UIButton *addFriendButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W/2+10, 10, 120, 50)];
    
    [addFriendButton setTitle:@"加好友" forState:UIControlStateNormal];
    [addFriendButton setTintColor:[UIColor blackColor]];
    [addFriendButton addTarget:self action:@selector(addFriendClick) forControlEvents:UIControlEventTouchUpInside];
    [addFriendButton setBackgroundColor:DarkGreencolor];
    
    addFriendButton.layer.cornerRadius = 5.0;
    addFriendButton.clipsToBounds = YES;
    
    [footerView addSubview:addFriendButton];
}

- (void)chatButtonClick {
    MemberModel *memberModel = _dataSource[0];
    User *user = [User sharedUser];
    if ([memberModel.user_id isEqualToString:user.employee_id]) {
        [SVProgressHUD showErrorWithStatus:@"不能给自己发信息"];
    }
    else {
        ChatController *chatVC = [[ChatController alloc] initWithChatter:memberModel.hx_username isGroup:NO];
        
        chatVC.customer_id = memberModel.user_id;
        chatVC.hx_username = memberModel.hx_username;
        chatVC.friendType = memberModel.type;
        chatVC.groupID = @"";
        chatVC.title = memberModel.truename;
        [self.navigationController pushViewController:chatVC animated:YES];
    }
}

- (void)addFriendClick {
    NSLog(@"加好友");
    User *user = [User sharedUser];
    
            EAHttpAPIClient *manager = [EAHttpAPIClient manager];
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            [SVProgressHUD show];
            
            NSDictionary *parameters = @{@"username":user.hx_username,
                                         @"friendname":huanxinID};
            NSLog(@"%@",parameters);
            [manager GetWithParameters:parameters method:@"set.hxuser.friend" success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSString *resposeString ;
                if ([responseObject isKindOfClass:[NSData class]]) {
                    resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
                }else
                    resposeString = responseObject;
                
                NSString *jsonString = [DataParser parseString:resposeString];
                id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
                NSLog(@"添加群好友的dictonary :%@",[dictionary description]);
                
                NSString *error = [dictionary objectForKey:@"error"];
                [SVProgressHUD showSuccessWithStatus:error];
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
                [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
            }];
            
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cellTextArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = _cellTextArray[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.textColor = [UIColor blackColor];

    cell.detailTextLabel.text = _cellDetailTextArray[indexPath.row];
    cell.detailTextLabel.textColor = [UIColor grayColor];
    cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
    cell.detailTextLabel.backgroundColor = [UIColor greenColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}



@end
